// void, Obj This, Obj bld

Hero this;
Unit u;
Building building;
point pt;
int i;

Sleep(50);
this = This.AsHero;
building = bld.AsBuilding;
//.pr("Hero::enter");   
if (!.IsValid)
	return;
if (!building.IsValid)
	return;
if (building.IsHeirOf("Catapult"))
	return;
if (.IsEnemy(building))
	return;
if (building.IsHeirOf("BaseRuins"))
	return;

.SetEntering(true);

if (!building.settlement.Units.Contains(this))
{
	
	pt = building.GetEnterPoint(this);
	.FormSetupAndMoveTo (pt, 0, 0, false);
	while (.HasPath && building.IsAlive)
	{                                     
		if (.FormPathLeft < .FormRadius  + 150)
			break;
		if (.DistTo(building) < .FormRadius)
			break;
		
		CreateFeedback("StaminaLoss", this);
		.army.ClearDead;
		for (i=0; i<.army.count; i+=1){
	 
			u = .army[i].AsUnit; 
			if(u.IsValid){
				if(u.IsAlive){
					Sleep(5);
					CreateFeedback("StaminaLoss", u);
					if(u.stamina > 0) u.SetStamina(0);
					if(u.health > 200) u.Damage(10);
				}
			}
		}
		if(.army.count >= 20){
			if (.race==Carthage || .race==Egypt)
				.PlaySound("data/Sound Entities/VOICERETREATARAB.XML");
			else if(.race==ImperialRome || .race==RepublicanRome)
				.PlaySound("data/Sound Entities/VOICERETREATROMAN.XML");
			else if(.race==Gaul || .race==Iberia || .race==Britain || .race==Germany)        
				.PlaySound("data/Sound Entities/VOICERETREATBARB.XML");
		}
		.FormKeepMoving (1500);
		continue;
	}
	if (!building.IsAlive)
	{
		.army.SetCommand("idle");
		.Stop(10000);
		return;
	}
	.army.SetCommand("enter", building);

	while(!.GotoEnter(pt, 0, 1000, false, 2500));

	if (!building.IsValid || .IsEnemy(building))
		{ .Stop(10000); .SetEntering(false); Sleep(rand(100)+100); return; }

	building.settlement.AddUnit(this);
}
else
	.army.SetCommand("enter", building);
	
.SetEntering(false);
